package com.demo.base.util;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.ArrayUtils;

public class CommonUtil {

	//错误
	public static String ERROR = "12";
	
	//成功
	public static String SUCCESS = "1";
	
	//失败
	public static String FAIL = "0";
	
	//服务器错误
	public static String SERVER_ERROR = "10";
	/**
	 * 数组合并 多个数组合并成一个
	 * 
	 * @param strs
	 * @return
	 */
	public static Object addArray(Object[]... strs) {
		Object[] rs = (Object[]) new Object[0];
		for (Object[] str : strs) {
			rs = (Object[]) ArrayUtils.addAll(rs, str);
		}
		return rs;
	}

	/**
	 * 判断对象是否为空，如果对象为ArrayList，且不为空，则还要判断是否有长度
	 * 
	 * @param obj
	 * @return
	 */
	public static boolean isNull(Object obj) {
		List list = null;
		String str = null;
		boolean flag = false;
		String[] strs = null;
		Long l;
		if (obj == null) {
			flag = true;
		} else {
			if (obj.getClass().getSimpleName().equals("ArrayList")) {
				list = (ArrayList) obj;
				if (list.size() == 0) {
					flag = true;
				}
			}
			if (obj.getClass().getSimpleName().equals("String[]")) {
				strs = (String[]) obj;
				if (strs.length == 1) {
					if (CommonUtil.isNull(strs[0])) {
						flag = true;
					}
				}
			}
			if (obj.getClass().getSimpleName().equals("String")) {
				str = (String) obj;
				if (str.equals("")) {
					flag = true;
				}
			}
			if (obj.getClass().getSimpleName().equals("Long")) {
				l = (Long) obj;
				if (l == 0) {
					flag = true;
				}
			}
		}
		return flag;
	}

	/**
	 * 随机生成X位字符加数据字的随机数。
	 * @param num 生成字符的位数。
	 * @return
	 */
	public static String generateCheckPass(int num) {
		String chars = "0123456789qwertyuiopasdfghjklzxcvbnm";
		String str = "";
		for (int i = 0; i < 6; i++) {
			int rand = (int) (Math.random() * 36);
			str = str + chars.charAt(rand);
		}
		return str;
	}

	/**
	 * 去掉空格 回车 换行
	 * @param str
	 * @return
	 */
	public static String replaceBlank(String str) {
		String dest = "";
		if (str != null) {
			Pattern p = Pattern.compile("\\s*|\t|\r|\n");
			Matcher m = p.matcher(str);
			dest = m.replaceAll("");
		}
		return dest;
	}

	/**
	 * 替换字符串中的单引号和双引号
	 * @param str
	 * @return
	 */
	public static String replaceDSYin(String str) {
		str = str.replaceAll("\"", "\\\\\"");
		str = str.replaceAll("\'", "\\\\\'");
		return str;
	}

	/**
	 * 发送RTX消息
	 * @param receiver 接收人
	 * @param sender 发送人
	 * @param msg 消息内容
	 */
	public static void sendMsgToRTX(String receiver, String sender, String msg) {
		try {
			String sendImg = "/SendNotify.cgi?title=" + URLEncoder.encode("德阳市一窗进出管理系统", "gbk"); // RTX发送消息接口
			String host = "10.5.115.16"; // RTX服务器地址
			// String getSessionkey = "/getsessionkey.cgi?";// RTX获取会话接口
			int port = 8012; // RTX服务器监听端口
			String content = msg + "[请登录系统：http://10.5.115.4]"; // 内容
			StringBuffer sendMsgParams = new StringBuffer(sendImg);

			sendMsgParams.append("&receiver=" + receiver);
			if (sender != null) {
				sendMsgParams.append("&sender=" + sender);
			}
			if (content != null) {
				sendMsgParams.append("&msg=" + URLEncoder.encode(content, "gbk"));
			}

			URL url = new URL("HTTP", host, port, sendMsgParams.toString());
			HttpURLConnection httpconn = (HttpURLConnection) url.openConnection();
			httpconn.getHeaderField(3);
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	/**
	 * 8位随机数
	 */
	public static String[] chars = new String[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };

	public static String generateShortUuid() {
		StringBuffer shortBuffer = new StringBuffer();
		String uuid = UUID.randomUUID().toString().replace("-", "");
		for (int i = 0; i < 8; i++) {
			String str = uuid.substring(i * 4, i * 4 + 4);
			int x = Integer.parseInt(str, 16);
			shortBuffer.append(chars[x % 0x3E]);
		}
		return shortBuffer.toString();

	}
}
